package chapter02.part04;

import chapter02.FileUtil;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;

/**
 * 使用 Files 代替文件输入输出流的部分繁琐操作
 * 
 * @author insight
 * @since 2021/8/18
 */
public class FileOpEasily {
    public static void main(String[] args) throws IOException {
        Path path = Paths.get(FileUtil.TEST_FILE_PATH);

        // 读
        Files.readAllBytes(path);
        Files.readString(path);
        Files.readAllLines(path);
        
        // 写
        String content = "Files.writeString()";
        Files.writeString(path, content, StandardCharsets.UTF_8);
        // 追加内容
        Files.write(path, content.getBytes(StandardCharsets.UTF_8),
                StandardOpenOption.APPEND);
        // 写集合
        Files.write(path, new ArrayList<String>(2), StandardCharsets.UTF_8);
        
        // 以上适合中等大小的文件
        // 大文件还是需要流
        Files.newInputStream(path);
        Files.newOutputStream(path);
        Files.newBufferedWriter(path);
        Files.newBufferedReader(path);
    }
}